package com.fm.controller;

import com.fm.baidu.FaceMatch;
import com.fm.utils.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.IOException;

@RestController
@RequestMapping("/api/baidu")
public class BaiduController {

    @RequestMapping(value = "/doFaceMatch", method = RequestMethod.POST)
    @ResponseBody
    public R doFaceMatch(@RequestParam("file") MultipartFile uploadFile) {
        // 将图片转为base64
        BASE64Encoder encoder = new BASE64Encoder();
        try {
            String imgData = encoder.encode(uploadFile.getBytes());
            String res = FaceMatch.search(imgData, "zly", "zly");
            return R.ok().put("data", res);
        }catch (IOException e) {
            e.printStackTrace();
            return R.error("图片转换失败");
        }
    }

//    @RequestMapping(value = "api/upload",method = RequestMethod.POST)
//    @ResponseBody
//    public AjaxResult SourceUpload(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request, HttpServletResponse response){
//        AjaxResult result = new AjaxResult(true);
//        String path = request.getSession().getServletContext().getRealPath("/resources/upload/");
//        String filename = uploadFile.getName();
//        String originalFilename = uploadFile.getOriginalFilename();
//        String type = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
//        String fileName = System.currentTimeMillis() + "." + type;
//        System.out.println("FileName = " + filename);
//        System.out.println("originalFilename = " + originalFilename);
//        if((!(type==null)) && (!(type.equals(""))))
//        {
//            File targetFile = new File(path, fileName);
//            try {
//                uploadFile.transferTo(targetFile);
//                result.setData(fileName);
//            } catch (IllegalStateException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        return result;
//    }
}
